package com.koron.order.grade.service.impl;

import com.koron.order.grade.bean.entity.GradeFormBean;
import com.koron.order.grade.bean.vo.GradeFormVO;
import com.koron.order.grade.mapper.GradeFormMapper;
import com.koron.order.grade.service.GradeFormService;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author twcao
 * @title: GradeFormServiceimpl
 * @projectName GDH_EAM_V2
 * @description: 验收单-表单
 * @date 2021/10/1214:31
 */
@Slf4j
@Service
public class GradeFormServiceImpl implements GradeFormService {

    /**
     * 保存验收单和表单关系
     * @param factory
     * @param beans
     * @return
     */
    @Override
    @TaskAnnotation("saveOrUpdate")
    public List<GradeFormVO> saveOrUpdate(SessionFactory factory, List<GradeFormBean> beans) {
        GradeFormMapper mapper = factory.getMapper(GradeFormMapper.class);
        beans.stream().forEach(bean -> mapper.deleteById(bean.getGradeId()));
        mapper.saveOrUpdate(beans);
        List<String> gradeIs = beans.stream().map(GradeFormBean::getGradeId).collect(Collectors.toList());
        List<GradeFormVO> gradeVos = new ArrayList<>();
        gradeIs.stream().forEach(gradeId -> gradeVos.addAll(mapper.queryList(new GradeFormVO().setGradeId(gradeId))));
        return gradeVos;
    }

    /**
     * 查询表单和验收单关系
     * @param factory
     * @param query
     * @return
     */
    @Override
    @TaskAnnotation("queryList")
    public List<GradeFormVO> queryList(SessionFactory factory, GradeFormVO query) {
        GradeFormMapper mapper = factory.getMapper(GradeFormMapper.class);
        return mapper.queryList(query);
    }

    @Override
    @TaskAnnotation("deleteByGradeId")
    public int deleteByGradeId(SessionFactory factory, String id) {
        GradeFormMapper mapper = factory.getMapper(GradeFormMapper.class);
        return mapper.deleteById(id);
    }
}
